<?php

namespace App\Http\Controllers\Frontend;

use App\Category;
use App\Post;
use Illuminate\Http\Request;

class PostController extends BaseController
{
    public function index(Request $request)
    {
        $column =$request->get('column');
        $cCategories = Category::where('parent_id', $column)->get();
        $cateId = $request->category ? $request->category : $cCategories[0]->id;
        $posts = Post::where('category_id', $cateId)->latest()->paginate(10);
        return view('frontend.post.index', compact(['cCategories', 'cateId', 'column', 'posts']));
    }

    public function show(Post $post)
    {
        $post->increment('pv');
        $post->attachments;
        $category = $post->category;
        $column = $category->parent_id;
        $cateId = $category->id;
        $cCategories = Category::where('parent_id', $column)->get();
        $next =  Post::where('id', '<', $post->id)->orderBy('id', 'desc')->where('category_id', $post->category_id)->first();
        $prev =  Post::where('id', '>', $post->id)->orderBy('id', 'desc')->where('category_id', $post->category_id)->first();
        return view('frontend.post.show', compact(['post', 'column', 'cateId', 'cCategories', 'next', 'prev']));
    }
}
